﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Way.EAP.DataAccess.Entity;
using Way.Bus.Model;
using Way.EAP.DataAccess.Data;
using System.Data;
using Way.Bus.WMS.Controllers;
using Way.Contract.MVC;

namespace Way.Bus.WMS.Model
{
    /// <summary>
    /// 描述：入库单明细表
    /// 作者：路聪
    /// 创建日期:2013-01-17
    /// </summary>
    [EntityClassAttribut("Acc_WMS_InNoticeMaterials", "入库通知明细", IsOnAppendProperty = true)]
    public class StockInNoticeMaterials : OrderMaterialsCommon, IEqualityComparer<StockInNoticeMaterials>
    {
        /// <summary>
        /// 来源单号
        /// </summary>
        [EntityControl("来源单号", true, true, 1)]
        [EntityForeignKey(typeof(StockInNotice), "ID", "CODE")]
        [EntityField(8)]
        public int PARENTID { get; set; }

        /// <summary>
        /// 推荐货位
        /// </summary>
        [EntityControl("推荐货位", true, true, 21)]
        [EntityField(50)]
        public string DEPOTWBS { get; set; }

        /// <summary>
        /// 货区
        /// </summary>
        [EntityControl("货区", true, true, 22)]
        [EntityField(255)]
        public string HQName { get; set; }

        //protected override string GetSearchSQL()
        //{
        //    StockInOrderMaterials siom = new StockInOrderMaterials();
        //    string bm = this.ToString();
        //    string sql = base.GetSearchSQL();
        //    string nsql = "select " + bm + ".*,isnull(b.FINISHNUM,0) FINISHNUM," + bm + ".num-isnull(b.FINISHNUM,0) STAYNUM from (" + sql + ") " + bm
        //        + " left join (select SourceRowID,sum(num) FINISHNUM from " + siom.ToString() + " group by  SourceRowID)  b on b.SourceRowID=" + bm + ".id";
        //    return nsql;
        //}
        protected override string GetSearchSQL()
        {
            StockInOrderMaterials siom = new StockInOrderMaterials();
            string bm = this.ToString();
            string sql = base.GetSearchSQL();
            string nsql = "select * from (select a.*,isnull(b.FINISHNUM,0) FINISHNUM,a.num-isnull(b.FINISHNUM,0) STAYNUM,c.CUSTOMEAN13 from (" + sql + ") a"
                + " left join (select SourceRowID,sum(num) FINISHNUM,materialcode from " + siom.ToString() + " group by  SourceRowID,materialcode)  b on b.SourceRowID=a.id and  b.MATERIALCODE = a.MATERIALCODE"
                + " left join Acc_Bus_BusinessCommodity c on c.id=a.MATERIALCODE) " + bm;
            return nsql;
        }
        public bool Equals(StockInNoticeMaterials x, StockInNoticeMaterials y)
        {
            if (x.ID == y.ID && x.MATERIALCODE == y.MATERIALCODE)
                return true;
            return false;
        }

        public int GetHashCode(StockInNoticeMaterials obj)
        {
            return this.GetHashCode();
        }

        private HierarchicalEntityView<StockInNoticeMaterials, InNoticeSequence> _InSequence;
        ///<summary>
        ///序列码
        ///</summary>
       //[HierarchicalEntityControl(isadd = false, isedit = false, ischeck = false)]
        public HierarchicalEntityView<StockInNoticeMaterials, InNoticeSequence> InSequence
        {
            get
            {
                if (_InSequence == null)
                {
                    _InSequence = new HierarchicalEntityView<StockInNoticeMaterials, InNoticeSequence>(this);
                }
                return _InSequence;
            }
        }
    }
    [EntityClassAttribut("WMS_InNoticeSequence", "序列号", IsOnAppendProperty = true)]
    public class InNoticeSequence : BusinessBase
    {
        /// <summary>
        /// 主表ID
        /// </summary>
        [EntityForeignKey(typeof(StockInNoticeMaterials), "PARENTID")]
        [EntityField(20)]
        public int MAINID { get; set; }
        /// <summary>
        /// 行ID
        /// </summary>
        [EntityForeignKey(typeof(StockInNoticeMaterials), "ID", "MATERIALCODE")]
        [EntityField(20)]
        public int PARENTID { get; set; }
        /// <summary>
        /// 序列码
        /// </summary>
        [EntityControl("序列号", false, true, 2)]
        [EntityField(200)]
        public string SEQUENCECODE { get; set; }
    }
}
